using System.Collections.Generic;
using ViScript.Core.ParsedSentence;

namespace ViScript.Core
{
    public interface ILanguageRule
    {
        IEnumerable<IParsedSentence> Parse(IEnumerable<ISentence> sentences);
    }

    public class LanguageRule : ILanguageRule, ISentenceParser
    {
        private readonly ISentenceRule[] sentenceRules;

        public LanguageRule(ISentenceRule[] sentenceRules)
        {
            this.sentenceRules = sentenceRules;
        }

        protected LanguageRule()
        {
        }

        public IEnumerable<IParsedSentence> Parse(IEnumerable<ISentence> sentences)
        {
            foreach (ISentence sentence in sentences)
            {
                yield return ParseSentence(sentence);
            }
        }

        public virtual IParsedSentence ParseSentence(ISentence sentence)
        {
            IParsedSentenceBuilder parsedSentenceBuilder = CreateParsedSentenceBuilder(sentence);
            foreach (ISentenceRule sentenceRule in sentenceRules)
            {
                parsedSentenceBuilder.Build(sentenceRule.ParseSentence(sentence));
            }
            return parsedSentenceBuilder.Result;
        }

        public virtual IParsedSentenceBuilder CreateParsedSentenceBuilder(ISentence sentence)
        {
            return new ParsedSentenceBuilder(sentence);
        }
    }
}